/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <QStringList>
#include <QTreeWidgetItem>

#include "aboutdialog.h"
#include "pluginbrowserdialog.h"

PluginBrowserDialog::PluginBrowserDialog(PluginManager *pluginManager,
    QWidget *parent) : QDialog(parent)
{
    m_pluginManager = pluginManager;

    setupUi(this);

    Q_FOREACH (QString pluginName, m_pluginManager->getAllPlugins()) {
        Plugin *plugin;

        plugin = m_pluginManager->getPlugin(pluginName);

        if (plugin) {
            QStringList columns;
            QString toolTip;

            // The first column is a check column.
            columns << "" << pluginName
                << plugin->getDescription() << plugin->getVersion();
            toolTip = pluginName + " - " + plugin->getVersion() + "\n\n"
                + plugin->getDescription();

            QTreeWidgetItem *insertItem = new QTreeWidgetItem(columns);

            // Set the tool tip for every column
            for (int i = 0; i < columns.size(); i++) {
                insertItem->setToolTip(i, toolTip);
            }

            // Check or uncheck if plugin is enabled/disabled
            if (plugin->isEnabled()) {
                insertItem->setCheckState(0, Qt::Checked);
            }
            else {
                insertItem->setCheckState(0, Qt::Unchecked);
            }

            pluginList->insertTopLevelItem(0, insertItem);
        }

        pluginList->sortItems(1, Qt::AscendingOrder);
    }

    // Select the firs item in the list.
    if (pluginList->selectedItems().isEmpty()
        && pluginList->topLevelItemCount() > 0)
    {
        pluginList->topLevelItem(0)->setSelected(true);
        pluginList->setCurrentItem(pluginList->topLevelItem(0));
    }
}

void PluginBrowserDialog::on_pluginList_itemChanged(QTreeWidgetItem *item,
    int column)
{
    // We don't care about the colum. We only care about the state of the item
    // because only the state of the check box in column 0 will change.
    Q_UNUSED(column);

    if (item->checkState(0) == Qt::Checked) {
        m_pluginManager->enablePlugin(item->text(1));
    }
    else if (item->checkState(0) == Qt::Unchecked) {
        m_pluginManager->disablePlugin(item->text(1));
    }

    on_pluginList_itemSelectionChanged();
}

void PluginBrowserDialog::on_pluginList_itemSelectionChanged()
{
    if (!pluginList->selectedItems().isEmpty()) {
        Plugin *plugin = m_pluginManager->getPlugin(pluginList->selectedItems()
            .at(0)->text(1));

        if (plugin->isEnabled()
            && plugin->getPluginInterface()->canConfig())
        {
            configureButton->setEnabled(true);
        }

        aboutButton->setEnabled(true);
    }
    else {
        configureButton->setEnabled(false);
        aboutButton->setEnabled(false);
    }
}

void PluginBrowserDialog::on_configureButton_clicked()
{
    if (!pluginList->selectedItems().isEmpty()) {
        QString pluginName = pluginList->selectedItems().at(0)->text(1);

        if (m_pluginManager->getPlugin(pluginName)->isEnabled()) {
            if (m_pluginManager->getPlugin(pluginName)->getPluginInterface()
                ->canConfig())
            {
                m_pluginManager->getPlugin(pluginName)->getPluginInterface()
                    ->execConfigDialog();
            }
        }
    }
}

void PluginBrowserDialog::on_aboutButton_clicked()
{
    if (!pluginList->selectedItems().isEmpty()) {
        QString pluginName = pluginList->selectedItems().at(0)->text(1);

        AboutDialog aboutDialog(
            m_pluginManager->getPlugin(pluginName)->getName()
            + " " + m_pluginManager->getPlugin(pluginName)->getVersion(),
            m_pluginManager->getPlugin(pluginName)->getDescription(),
            m_pluginManager->getPlugin(pluginName)->getCredits(),
            m_pluginManager->getPlugin(pluginName)->getLicense(), this);

        aboutDialog.exec();
    }
}
